# -*- coding: utf-8 -*-
import maya.cmds as cmds
import maya.mel as mel
import os
import pymel.core as pm
class Check_UnknownNode():
	def Check_UnknownNode(self):
		cmds.evaluationManager (mode = "off")
		self.CleanFile()
		log = ''
		unknownNodeList = cmds.ls(type="unknown")
		if unknownNodeList:
			cmds.lockNode(unknownNodeList, l=0)
			cmds.delete(unknownNodeList)

		unknownDagList = cmds.ls(type="unknownDag")
		if unknownDagList:
			cmds.delete(unknownDagList)

		loaded_plugins = cmds.unknownPlugin(query=True, list=True)
		if loaded_plugins:
			for plugin in loaded_plugins:
				cmds.unknownPlugin(plugin, remove=True)





		unknownDagList = cmds.ls (type="unknownDag")
		if len(unknownDagList) :

			log += "unknownDag-NO,"
		else :
			log += "unknownDag-YES,"

		loaded_plugins = cmds.unknownPlugin ( query=True, list=True )
		if loaded_plugins :
			log += "unknownPlugin-NO,"
		else:
			log += "unknownPlugin-YES,"


		unknownNodeList = cmds.evalDeferred('cmds.ls (type="unknown")')
		if  unknownNodeList:
			log += "unknown-NO,"
		else :
			log += "unknown-YES,"

		refFile_Node = []
		refNode = cmds.ls (type="reference")
		refFile = cmds.file (q=True, r=True)

		for i in range (len (refFile)) :
			node = cmds.referenceQuery (refFile[i], referenceNode=True)
			refFile_Node.append (node)
			childRefNodes = cmds.referenceQuery (refFile[i], child=True, referenceNode=True)
			refFile_Node.append (childRefNodes)

		result_list = list (set (refNode) - set (refFile_Node)) + list (set (refFile_Node) - set (refNode))
		errNode = [item for item in result_list if item]
		if len (errNode) > 0 :
			for i in errNode :
				cmds.lockNode (i, l=False)
				cmds.delete (i)


		return (log)

	def CleanFile(self) :
		myScriptDir = cmds.internalVar (userAppDir = True) + "scripts/"
		# if myScriptDir not in sys.path:
		# sys.path.append(myScriptDir)
		Vaccine = cmds.objExists ('vaccine_gene')
		if (Vaccine == True) :
			cmds.delete ('breed_gene')
			cmds.delete ('vaccine_gene')

			mmp01 = myScriptDir + "userSetup.py"
			mmp02 = myScriptDir + "vaccine.py"
			mmp03 = myScriptDir + "vaccine.pyc"
			if os.path.exists (mmp01) :
				os.remove (mmp01)
			if os.path.exists (mmp02) :
				os.remove (mmp02)
			if os.path.exists (mmp03) :
				os.remove (mmp03)
			cmds.warning (u"!!!!!A:文件中 感染代码已清除>>>> ")
		else :
			cmds.warning (u'未发现病毒')
		for item in pm.lsUI (editors = True) :
			if isinstance (item , pm.ui.ModelEditor) :
				pm.modelEditor (item , edit = True , editorChanged = "")
